# 通过`datetime`模块完成时间戳, `datetime.datetime`对象, 格式化字符串三者之间的转换
# 封装一个函数`get_date(day_delta)`, 如果传入的是`-1` , 输出就是字符串日期`2020-01-02`.

import datetime
import time
print("time.time():     ",time.time()) #1609853871.385191  单位秒，毫秒、微秒#英国，本初子午线，格林威治时间

now00=list(time.localtime())
print(now00)
print(type(now00))
# [2021, 1, 6, 11, 51, 28, 2, 6, 0]
# <class 'list'>

now = time.localtime()
print("now:   ",now)#time.struct_time(tm_year=2021, tm_mon=1, tm_mday=5, tm_hour=21, tm_min=47, tm_sec=36, tm_wday=1, tm_yday=5, tm_isdst=0)
print(type(now))   #<class 'time.struct_time'>

# <class 'time.struct_time'>----->>> <class 'str'>
now1 = time.strftime("%Y-%m-%d %H:%M:%S", now)
print("now1:     ",now1)  #2021-01-05 21:47:36
print(type(now1))  #<class 'str'>

# <class 'str'>----->>> <class 'datetime.datetime'>
now2 = datetime.datetime.strptime(now1, "%Y-%m-%d %H:%M:%S")
print("now2:     ",now1)  #2021-01-05 21:47:36
print(type(now2))  #<class 'datetime.datetime'>

# <class 'datetime.datetime'>----->>> <class 'float'>
now3=now2.timestamp()
print("now3:     ",now3)  # 1609902463.0
print(type(now3))  #<class 'float'>

# <class 'float'>----->>> <class 'datetime.datetime'>
now4=datetime.datetime.fromtimestamp(now3, tz=None)
print("now4:     ",now4)  #2021-01-06 11:11:31
print(type(now4))  #<class 'datetime.datetime'>


# 封装一个函数`get_date(day_delta)`, 打印输出当前日期的后面n天
# 如果传入的是`-1` , 输出就是**字符串**日期`2020-01-02`.今天是0103；

def get_date(n):
    d = datetime.datetime.today()
    if  n==0:
        date = d
    else:
        date = d + datetime.timedelta(days=n)
    date_str=date.strftime("%Y-%m-%d ")
    return  date_str

if __name__=="__main__":
    n1=-3
    n2=0
    n3=3
    print(get_date(n1))   #<class 'str'>
    print(get_date(n2))
    print(get_date(n3))

    #输出结果：
    # 2021 - 01 - 03
    # 2021 - 01 - 06
    # 2021 - 01 - 09